package com.wxx.service.bg;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.wxx.common.Const;
import com.wxx.mapper.DicAclMapper;
import com.wxx.mapper.DicRoleMapper;
import com.wxx.mapper.DicUserMapper;
import com.wxx.model.DicAcl;
import com.wxx.model.DicRole;
import com.wxx.model.DicUser;
import com.wxx.util.AppUtil;
import com.wxx.util.JsonData;
import com.wxx.vo.DicAclVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DicUserServiceImpl implements DicUserService {

    @Autowired
    private DicUserMapper dicUserMapper;
    @Autowired
    private DicAclMapper dicAclMapper;
    @Autowired
    private DicRoleMapper dicRoleMapper;

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public Map edit(DicUser dicUser) {
        //判断修改的和传过来的登录名是否一致，如果一致也是可以保存的
        DicUser dicUser1 = dicUserMapper.selectByPrimaryKey(dicUser.getUid());

        //修改的和传过来的登录名不一致在判断是否存在相同的登录名
        if ((!dicUser1.getLoginName().equals(dicUser.getLoginName()) ) && StringUtils.isNotBlank(dicUser.getLoginName())) {
            //查询登录名是否存在,存在返回错误
            if (loginNameIsExist(dicUser)) {
                return JsonData.returnMap(0, "登录名已存在");
            }
        }

        dicUser.setUpdateTime(new Date());
        if (dicUser.getLoginPwd() != "") {
            dicUser.setLoginPwd(AppUtil.md5Salt(dicUser.getLoginPwd()));
        }else{
            //传过来的是"" 代表不修改密码
            dicUser.setLoginPwd(null);
        }
        //993de69b42d0e0a6d5b8031418c6de98
        int i = dicUserMapper.updateByPrimaryKeySelective(dicUser);
        return JsonData.returnMap(i);
    }

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public Map goEdit(String uid) {
        DicUser dicUser = dicUserMapper.selectByPrimaryKey(uid);
        List<DicRole> excludeAdmin = dicRoleMapper.findDicRoleListByExcludeAdmin();
        Map map = Maps.newHashMap();
        map.put("dicUser",dicUser);
        map.put("excludeAdmin",excludeAdmin);
        return map;
    }

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public Map del(String uid) {
        int i = dicUserMapper.deleteByPrimaryKey(uid);
        return JsonData.returnMap(i);
    }

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public Map add(DicUser dicUser) {
        //查询登录名是否存在,存在返回错误
        if (loginNameIsExist(dicUser)){
            return JsonData.returnMap(0, "登录名已存在");
        }


        dicUser.setUid(AppUtil.uuid());
        dicUser.setLoginPwd(AppUtil.md5Salt(dicUser.getLoginPwd()));
        dicUser.setUserStatus(0);
        //注册时间
        dicUser.setCreateTime(new Date());
        int i = dicUserMapper.insertSelective(dicUser);
        System.out.println(i);
        return JsonData.returnMap(i);
    }

    private boolean loginNameIsExist(DicUser dicUser) {
        DicUser model = new DicUser();
        model.setLoginName(dicUser.getLoginName());
        int flag = dicUserMapper.selectCount(model);
        if (flag > 0){
            return true;
        }
        return false;
    }

    @Transactional(propagation= Propagation.SUPPORTS)
    @Override
    public List<DicUser> findDicUserList(String roleName, Integer page, Integer limit) {
        PageHelper.startPage(page,limit,false);
        return dicUserMapper.findDicUserList(roleName);
    }


    @Transactional(propagation= Propagation.SUPPORTS)
    @Override
    public Map login(String loginName, String loginPwd) {

        DicUser dicUser = new DicUser();
        dicUser.setLoginName(loginName);
        dicUser.setLoginPwd(loginPwd);
        DicUser user = dicUserMapper.selectOne(dicUser);
        if (user != null){
            //获取角色信息
            DicRole dicRole = dicRoleMapper.selectByPrimaryKey(user.getRoleId());

            //登录成功 查询所有菜单权限
            List<DicAcl> dicAclList = dicAclMapper.selectAllMenu();

            //将所有权限按照parentId分组
            Multimap<String, DicAcl> multimap = ArrayListMultimap.create();
            for (DicAcl dicAcl:dicAclList){
                multimap.put(dicAcl.getParentId(),dicAcl);
            }
            //获取顶级父节点
            List<DicAcl> dicAcls = (List<DicAcl>) multimap.get("0");

            List<DicAclVo> aclVos = Lists.newArrayList();
            //遍历顶级父节点，查到相应的子节点集合，并放入一个组装集合中
            for (DicAcl dicAcl : dicAcls) {
                DicAclVo aclVo = new DicAclVo();
                aclVo.setAcl(dicAcl);
                List<DicAcl> aclList = (List<DicAcl>) multimap.get(dicAcl.getAclId());
                //aclList 排序
                aclList.sort((o1, o2) -> o1.getSort()-o2.getSort());
                aclVo.setList(aclList);
                aclVos.add(aclVo);
            }

            dicAcls.sort((o1, o2) -> o1.getSort());

            //查询权限路径集合
            List<String> allURIByRoleId = dicAclMapper.findAllURIByRoleId(user.getRoleId());
            Map<String, Object> map = Maps.newHashMap();
            map.put("code", Const.SUCCESS_CODE);
            map.put("aclVos",aclVos);
            map.put("user",user);
            map.put("dicRole",dicRole);
            map.put("allURIByRoleId",allURIByRoleId);

            return map;
        }
        return JsonData.fail("账号或密码有误");
    }
}
